package cn.edu.ctbu.yrx.service;

import cn.edu.ctbu.yrx.constant.REnum;
import cn.edu.ctbu.yrx.entity.User;
import cn.edu.ctbu.yrx.dao.UserRepository;
import cn.edu.ctbu.yrx.rexception.RException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public User validateUsernameAndPassword(String name, String password) throws Exception {


        List<User> users = userRepository.findByUsername(name);
        if (users.size() > 0) {
            User user = users.get(0);
            if (user.getPassword().equals(password)) {

                //成功
                return user;
            } else {
                throw new RException(REnum.LOGIN_ERR);
            }

        } else {
            throw new RException(REnum.LOGIN_ERR);
        }
    }
}
